package com.quanyue.mechanics.complex.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.mechanics.complex.service.ProductService;
import com.quanyue.mechanics.complex.type.ProductReq;
import com.quanyue.mechanics.domain.entity.Product;
import com.vhuomai.mybatis.mapper.page.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@Api(tags = "商品")
@RestController
@RequestMapping("/api")
public class ProductController {
    @Autowired
    private ProductService productService;

    @ApiOperation(value = "添加商品")
    @PostMapping(value = "/product", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Product> addProduct(@RequestBody Product product) {
        return ServiceResult.success(productService.addProduct(product));
    }

    @ApiOperation(value = "修改商品")
    @PutMapping(value = "/product", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> updateProduct(@RequestBody Product product) {
        productService.updateProduct(product);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除商品")
    @DeleteMapping(value = "/product", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> deleteProduct(@RequestParam("id") Integer id) {
        productService.deleteProduct(id);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "商品列表")
    @PostMapping(value = "/product/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Page <Product>> findProduct(@RequestBody ProductReq req) {
        return ServiceResult.success(productService.findProductByMenu(req));
    }
}
